import 'package:flutter/material.dart';
import 'package:flutter_app/r.dart';
import 'package:flutter_app/ui/welcome/welcome_view.dart';
import 'package:flutter_app/utils/sp_utils.dart';
import 'package:flutter_app/values/CommonParam.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/MyTabPageSelector.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';

/**
 * @Description: 引导页面
 * @Author: 下雨了
 * @CreateDate: 2022-05-18 13:36
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-05-18 13:36
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class GuidePage extends StatefulWidget {
  final String title;

  const GuidePage({Key? key, required this.title}) : super(key: key);

  @override
  _GuidePageState createState() => _GuidePageState();
}

class _GuidePageState extends State<GuidePage>
    with SingleTickerProviderStateMixin {
  List<String> text = [
    R.assetsImgGuide51,
    R.assetsImgGuide61,
    R.assetsImgGuide71,
    R.assetsImgGuide81
  ];

  List<String> images = [
    R.assetsImgGuide52,
    R.assetsImgGuide62,
    R.assetsImgGuide72,
    R.assetsImgGuide82
  ];

  late TabController _tabController;

  late List<Widget> listImage = [];

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(initialIndex: 0, length: images.length, vsync: this);

    for (int i = 0; i < images.length; i++) {
      listImage.add(
        Stack(
          children: [
            Container(
              width: double.infinity,
              height: double.infinity,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [Colors.white, MyColors.FFF8EAE6],
                ),
              ),
            ),

            Align(
              alignment: Alignment.center,
              child: Container(
                margin: EdgeInsets.only(top: 100, right: 0, left: 0),
                child: Image.asset(
                  images[i],
                  fit: BoxFit.fitWidth,
                ),
              ),
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                margin: EdgeInsets.only(top: 100),
                child: SizedBox(
                  height: 100,
                  child: Image.asset(
                    text[i],
                    fit: BoxFit.fitHeight,
                  ),
                ),
              ),
            ),
            Visibility(
              visible: i == (images.length - 1),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: InkWell(
                  child: Container(
                    alignment: Alignment.center,
                    width: 135,
                    height: 45,
                    margin: EdgeInsets.only(bottom: 45),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5),
                      gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [MyColors.FFF56956, MyColors.FFF04035],
                      ),
                    ),
                    child: Text("立即体验", style: TextStyle(color: Colors.white, fontSize: 18),),
                  ),
                  onTap: () {
                    _goToWelcomePage();
                  },
                ),
              ),
            )
          ],
        )
      );
    }
  }

  /// 跳转欢迎页面
  _goToWelcomePage() async {
    await sp.setBool(CommonParam.FIRST_OPEN, true);
    Get.offAll(() => WelcomePage(title: '欢迎页面'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          TabBarView(
            controller: _tabController,
            children: listImage,
          ),
          Container(
            margin: EdgeInsets.only(top: 45, right: 30),
            child: Align(
              alignment: Alignment.topRight,
              child: SizedBox(
                width: 60,
                height: 30,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    padding: EdgeInsets.all(0),
                    primary: MyColors.FFCBCBCB,
                    shape: StadiumBorder(),
                  ),
                  onPressed: () {
                    _goToWelcomePage();
                  },
                  child: Text("跳过", style: TextStyle(color: Colors.white, fontSize: 14),),
                ),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 15),
            child: Align(
              alignment: Alignment.bottomCenter,
              child: MyTabPageSelector(
                controller: _tabController,
                selectedColor: MyColors.FFE5261E,
                indicatorSize: 10,
                color: MyColors.FFE3E3E3,
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }
}
